
#include <BMenu>
#include <BLabel>
#include <BGridLayout>
#include <BHBoxLayout>
#include <BVBoxLayout>
#include <BColorDialog>
#include <BFileDialog>
#include <BReadWrite>
#include <BApplication>

#include "LabelLayerPanel.h"

LabelLayerPanel::LabelLayerPanel(BWidget* parent) : BGroupBox(parent)
{
	this->setTitle("Label");

	_label = 0;

	BGridLayout* gridLayout = new BGridLayout();
	{
		_formComboBox = new BComboBox();
		_formComboBox->addItem("None", Form_None);
		_formComboBox->addItem("TextAlone", Form_TextAlone);
		_formComboBox->addItem("TextOnly", Form_TextOnly);
		_formComboBox->addItem("IconAlone", Form_IconAlone);
		_formComboBox->addItem("IconOnly", Form_IconOnly);
		_formComboBox->addItem("IconText", Form_IconText);
		_formComboBox->addItem("TextIcon", Form_TextIcon);
		_formComboBox->addItem("TextUnderIcon", Form_TextUnderIcon);
		_formComboBox->addItem("IconUnderText", Form_IconUnderText);

		_alignButton = new BButton();
		{
			BMenu* menu = new BMenu();
			_alignWidget = new AlignWidget(_alignButton);
			BLayout* layout = new BLayout(menu);
			layout->setWidget(_alignWidget);
			_alignButton->setMenu(menu);
		}

		_imageButton = new BButton(Form_IconText);

		BHBoxLayout* imageSizeLayout = new BHBoxLayout();
		{
			_imageWidthSpinBox = new BLongSpinBox(-1, 999999);
			_imageHeightSpinBox = new BLongSpinBox(-1, 999999);
			imageSizeLayout->addWidget(_imageWidthSpinBox);
			imageSizeLayout->addWidget(_imageHeightSpinBox);
		}

		gridLayout->addWidget(1, 0, new BLabel("form"));
		gridLayout->addWidget(1, 1, _alignButton);
		gridLayout->addWidget(3, 0, new BLabel("image"));
		gridLayout->addWidget(3, 1, _imageButton);
		gridLayout->addWidget(4, 0, new BLabel("image size"));
		gridLayout->addLayout(4, 1, imageSizeLayout);
		gridLayout->setColumnStretch(0, 0);
	}

	_textEdit = new BTextEdit("Text");

	BVBoxLayout* vlayout = new BVBoxLayout(this);
	vlayout->addLayout(gridLayout);
	vlayout->addWidget(_textEdit);

	connect(this, Signal_AssetChanged, &LabelLayerPanel::slotAssetChanged);

	connect(_formComboBox, Signal_ItemClicked, &LabelLayerPanel::slotFormComboItemClicked);
	connect(_alignWidget, Signal_AlignChanged, &LabelLayerPanel::slotAlignChanged);
	connect(_imageButton, Signal_Clicked, &LabelLayerPanel::slotImageButtonClicked);
	connect(_textEdit, Signal_EditFinished, &LabelLayerPanel::slotTextEditFinished);
	connect(_imageWidthSpinBox, Signal_EditFinished, &LabelLayerPanel::slotImageSizeEditFinished);
	connect(_imageHeightSpinBox, Signal_EditFinished, &LabelLayerPanel::slotImageSizeEditFinished);

}
LabelLayerPanel::~LabelLayerPanel()
{

}

void LabelLayerPanel::slotAssetChanged(BObject* object, const BValue& value)
{
	BLabel* label = this->asset();
	if (_label != label)
	{
		_label = label;
		this->setEnabled(_label);
		if (_label)
		{
			_formComboBox->setValue(_label->form());
			_alignWidget->setAlign(_label->align());
			_alignButton->setText(_alignWidget->alignText());
			_textEdit->setText(_label->text());
			_imageButton->setImage(_label->image());
			_imageWidthSpinBox->setValue(_label->imageSize().width());
			_imageHeightSpinBox->setValue(_label->imageSize().height());
		}
	}
}
void LabelLayerPanel::slotFormComboItemClicked(BObject* object, const BValue& value)
{
	BComboItem* item = value;
	_label->setForm(item->value());
}
void LabelLayerPanel::slotAlignChanged(BObject* object, const BValue& value)
{
	Align align = value;
	_label->setAlign(align);
}
void LabelLayerPanel::slotImageButtonClicked(BObject* object, const BValue& value)
{
	BString fileName = BFileDialog::OpenFile("Choose image file", "", ".png;jpeg;jpg;tiff;tif;bmp;");
	if (fileName.size())
	{
		BImage* image = ReadFile<BImage>(fileName);
		fileName.remove(BApplication::currentPath());
		fileName.removeHead('/');
		const BString& fname = image->fileName();
		_label->setImage(image);
		_imageButton->setText(fileName);
	}
}
void LabelLayerPanel::slotImageArrowClicked(BObject* object, const BValue& value)
{

}
void LabelLayerPanel::slotTextEditFinished(BObject* object, const BValue& value)
{
	const BString& text = value;
	_label->setText(text);
}
void LabelLayerPanel::slotImageSizeEditFinished(BObject* object, const BValue& value)
{
	int width = (int)_imageWidthSpinBox->value();
	int height = (int)_imageHeightSpinBox->value();
	_label->setImageSize(width, height);
}

